package vn.cent.game.cardtype.main;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import vn.cent.game.cardtype.bc.BCBean;
import vn.cent.game.cardtype.client.*;
import vn.cent.game.cardtype.server.*;
import vn.cent.game.cardtype.utils.CCommand;








import vn.cent.game.cardtype.utils.SCommand;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.extensions.SFSExtension;
import com.smartfoxserver.v2.core.SFSEventType;

public class CardTypeGameExtension extends SFSExtension {
	
	private ConcurrentHashMap<Integer, BCBean> listGameBC = null;
	
	
	@Override
	public void init()
	{
		trace("[CardExtension] Start init");
		// Add server event handler
		addEventHandler(SFSEventType.USER_JOIN_ROOM, UserJoinRoom.class);
		// Done implement
		addEventHandler(SFSEventType.USER_LEAVE_ROOM, UserLeaveRoom.class);
		// Done implement
		addEventHandler(SFSEventType.ROOM_REMOVED, RoomRemoved.class);
		// Done implement
		addEventHandler(SFSEventType.USER_DISCONNECT, UserDisconnect.class);
		
		// Add client request handler
		// Refactor code
		addRequestHandler(CCommand.REQUEST_QUICK_GAME, RequestQuickGame.class);
		addRequestHandler(CCommand.REQUEST_JOIN_CREATED_ROOM, RequestJoinCreatedRoom.class);
		addRequestHandler(CCommand.REQUEST_CREATE_NEW_ROOM, RequestCreateNewRoom.class);
		
		
		// Temp old code.
		addRequestHandler(CCommand.MAKE_READY, MakeReady.class);
		addRequestHandler(CCommand.MAKE_NOT_READY, MakeNotReady.class);
		addRequestHandler(CCommand.MAKE_START, MakeStart.class);
		addRequestHandler(CCommand.MAKE_ROLL, MakeRoll.class);
		addRequestHandler(CCommand.GOT_ROLL_RESULT, GotRollResult.class);
		addRequestHandler(CCommand.DONE_UPDATE, DoneUpdate.class);
		addRequestHandler(CCommand.SET_HOST, MakeReady.class);
		
		// Room create request
		addRequestHandler(CCommand.MAKE_ROOM, MakeRoom.class);
		
	}
	
	@Override
	public void destroy()
	{
		super.destroy();
		
		// Remove event listener
		removeEventHandler(SFSEventType.USER_JOIN_ROOM);
		removeEventHandler(SFSEventType.USER_LEAVE_ROOM);
		removeEventHandler(SFSEventType.ROOM_REMOVED);
		removeEventHandler(SFSEventType.USER_DISCONNECT);
		
		removeRequestHandler(CCommand.MAKE_READY);
		removeRequestHandler(CCommand.MAKE_NOT_READY);
		removeRequestHandler(CCommand.MAKE_START);
		removeRequestHandler(CCommand.MAKE_ROLL);
		removeRequestHandler(CCommand.GOT_ROLL_RESULT);
		removeRequestHandler(CCommand.DONE_UPDATE);
		
	}
	
	
	public void StartGame(List<User> recipients)
	{
		trace("[CardExtension] Start game");
		for (User user: recipients){
			if (!user.isPlayer())
			{
				trace("Player name: " + user.getName() + " has left the game");
				return;
			}
		}
		
		// Send command to all player
		send(SCommand.READY_START, null, recipients);
	}
	
	void HandleGameReady()
	{
		
	}

	public ConcurrentHashMap<Integer, BCBean> getListGameBC() {
		return listGameBC;
	}

	public void setListGameBC(ConcurrentHashMap<Integer, BCBean> listGameBC) {
		this.listGameBC = listGameBC;
	}
	
	
}
